﻿AB.namespace("SD_SaleOrderIndex.Mgr");
SD_SaleOrderIndex.Mgr = {
    commandHandler: function (command, record) {
        var FId = record.data.FId;
        switch (command) {
            case "Edit":
                FIdH.setValue(FId);
                SaleOrderEidtStore.load();
                SaleOrderDetailListStore.load();
                break;
            case "Delete":
                
                break;
        }
    },
    successHandler: function (form, action) {
        //successHandler(form, action, this.successCallback);
    },
    failureHandler: function (form, action, failureCallback) {
        failureHandler(form, action);
    },
    successCallback: function () {
    },
    failureCallback: function () {
    },
    formStoreLoaded: function (store, records) {
        var formObj = Ext.getCmp("EditFormPanel");
        formStoreLoaded(store, records, formObj);
        OrderStatusStore.load();
        BillTypeStore.load();
    },
    searchList: function () {
        SaleOrderListStore.reload();
    },
    saveReceiveMsg: function () {
        if (FBillDate.getValue() == "") {
            Ext.Msg.show({
                title: '操作失败',
                msg: '请选择销售日期!',
                buttons: Ext.Msg.OK,
                icon: Ext.Msg.ERROR
            });
            return;
        }

        var FIdT = FIdH.getValue();
        var FNumberT = FNumber.getValue();
        var FBillDateT = Ext.util.Format.date(FBillDate.getValue(), 'Y-m-d');
        var FBusinessCategoryIdT = FBusinessCategoryId.getValue();
        var FCustomerNameT = FCustomerName.getValue();
        var FCustomerMobileT = FCustomerMobile.getValue();
        var FCustomerPhoneT = FCustomerPhone.getValue();
        var FCustomerAddressT = FCustomerAddress.getValue();
        var FRemarkT = FRemark.getValue();

        var FDetails = "";
        var view = SaleOrderDetailGrid.getView().ds;
        if (view != null) {
            var mark = view.data.items;
            for (var i = 0; i < mark.length; ++i) {

                if (i == mark.length - 1) {
                    FDetails += "{FId:'" + mark[i].data.FId + "',FBarcode:'" + mark[i].data.FBarcode + "'}";
                }
                else {
                    FDetails += "{FId:'" + mark[i].data.FId + "',FBarcode:'" + mark[i].data.FBarcode + "'},";
                }
            }
        }

        var msg = "{FId:'" + FIdT
                    + "',FNumber:'" + FNumberT
                    + "',FBillDate:'" + FBillDateT
                    + "',FBusinessCategoryId:'" + FBusinessCategoryIdT
                    + "',FCustomerName:'" + FCustomerNameT
                    + "',FCustomerMobile:'" + FCustomerMobileT
                    + "',FCustomerPhone:'" + FCustomerPhoneT
                    + "',FCustomerAddress:'" + FCustomerAddressT
                    + "',FRemark:'" + FRemarkT
                    + "',FDetails:[" + FDetails + "]}";

        Ext.Ajax.request({
            url: "/SaleOrder/SaveSaleOrderMsg",
            method: 'post',
            params: {
                msg: msg
            },
            success: function (result) {
                if (!isTimeOut(result)) {
                    var response = Ext.util.JSON.decode(result.responseText);
                    if (response.success == false) {
                        ajaxFailureHandler(response);
                    } else {
                        FIdH.setValue(response.result);
                        SaleOrderEidtStore.reload();
                        SaleOrderDetailListStore.reload();
                    }
                }
            }
        });
    },
    deleteReceiveOrder: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        Ext.Msg.confirm('提示', '确定要删除该记录吗?', function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url: "/ReceiveOrder/DeleteReceiveOrder",
                    method: 'post',
                    params: {
                        FId: FIdT
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                FIdH.setValue("");
                                EditFormPanel.form.reset();

                                var mark = OrderStatusStore.data.items;
                                FStatus.setValue(mark[0].data.FID);

                                EditReceiveOrderDetailStore.removeAll();
                                ReceiveOrderListStore.reload();
                            }
                        }
                    }
                });
            }
        });
    },
    resetEditData: function () {
        FIdH.setValue("");
        EditFormPanel.form.reset();

        var mark = OrderStatusStore.data.items;
        FStatus.setValue(mark[0].data.FID);

        EditReceiveOrderDetailStore.removeAll();
    },
    resetSearchData: function () {
        SearchFormPanel.form.reset();
    },
    approveReceiveOrder: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        Ext.Msg.confirm('提示', '确定要审核该销售订单吗?', function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url: "/SaleOrder/ApproveOrder",
                    method: 'post',
                    params: {
                        FId: FIdT
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                Ext.Msg.alert('操作信息', "审核成功!");
                            }
                        }
                    }
                });
            }
        });
    },
    unApproveReceiveOrder: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        Ext.Msg.confirm('提示', '确定要反审核该销售订单吗?', function (btn) {
            if (btn == "yes") {
                Ext.Ajax.request({
                    url: "/SaleOrder/UnApproveSaleOrder",
                    method: 'post',
                    params: {
                        FId: FIdT
                    },
                    success: function (result) {
                        if (!isTimeOut(result)) {
                            var response = Ext.util.JSON.decode(result.responseText);
                            if (response.success == false) {
                                ajaxFailureHandler(response);
                            } else {
                                Ext.Msg.alert('操作信息', "反审核成功!");
                            }
                        }
                    }
                });
            }
        });
    },
    PrintBarcode: function () {
        var FIdT = FIdH.getValue();
        if (FIdT == "") {
            return;
        }
        window.open("/ReceiveOrder/PrintBarcode?FId=" + FIdT);
    }
}

var showDetailMsg = function (item,msg) {
    item.setValue(msg);
}